<template>
	<view class="price-container">
		<view class="container-price">
			<view class="price-left">
				<text class="nav-jq0">￥</text>
				<text>{{price}}/小时</text>
			</view>
		</view>
		<view class="container-peopleNumber-data">
			<view class="price-peopleNmber">
				<text>可容纳人数：{{peopleNumber}}人</text>
			</view>
<!-- 			<view class="price-data">
				<text>预约时段：早上8:00~晚上8:00</text>
			</view> -->
		</view>
		<view class="uni-title uni-common-pl">
			<a style="color: #ff0000;">*</a>选择预约日期</view>
		<uni-datetime-picker @change="timeChange" :disabled="false" :start="startDate" :end="endDate" :value="currentDate" type="date" />
        <view class="uni-title uni-common-pl">
			<a style="color: #ff0000;">*</a>选择开始时间</view>
        <view class="uni-list">
            <view class="uni-list-cell">
                <view class="uni-list-cell-left">
                    当前选择
                </view>
                <view class="uni-list-cell-db">
                    <picker :disabled="state !== '预约'" @change="bindPickerChange" :value="startSelDate" :range="startTimeList">
                        <view class="uni-list-cell-left">{{startTimeList[indexStart]}}</view>
                    </picker>
                </view>
            </view>
        </view>
		<view class="uni-title uni-common-pl">
			<a style="color: #ff0000;">*</a>选择结束时间</view>
		<view class="uni-list">
		    <view class="uni-list-cell">
		        <view class="uni-list-cell-left">
		            当前选择
		        </view>
		        <view class="uni-list-cell-db">
		            <picker :disabled="state !== '预约'" @change="bindPickerChangeEnd" :value="endSelDate" :range="endTimeList">
		                <view class="uni-list-cell-left">{{endTimeList[indexEnd]}}</view>
		            </picker>
		        </view>
		    </view>
		</view>
	</view>
</template>

<script>
	import {
		processHistoryList,
		getNowFormatDate,
		getFutureFormatDate,
	} from "../../util/common.js"
	import {
		ONLINE_SERVER_ADDRESS_BASE,
	} from "../../util/network.js"
	export default {
		data() {
			return {
				array: ['8:00 ~ 12:00', 
				'13:00 ~ 17:00', 
				'18:00 ~ 22:00' 
				],
				indexStart: 0,
				indexEnd: 0,
				currentDate: "",
				startDate:"",
				endDate:"",
				startTimeList:[],
				startIndex:0,
				endIndex:0,
				endTimeList:[],
				token:"",
				availableDurationList:[],
			}
		},
		props: {
			state: String,
			price: Number,
			peopleNumber: String,
			startSelDate: {
				type:String,
				default:"2021-04-3"
			},
			endSelDate: {
				type:String,
				default:"2021-04-3"
			},
			roomId:String,
		},
		mounted() {
			this.startDate = getNowFormatDate()
			this.token = uni.getStorageSync("token")
		},
		methods:{
			initDate(date){
				this.endDate = getFutureFormatDate(date)
			},
			getStartTimeList(date){
				this.startTimeList = []
				let flag = false
				for (var i = 0; i < this.availableDurationList.length; i++) {
					if(flag){
						if(this.availableDurationList[i].startTime.indexOf(date) == -1) break
					}
					if(this.availableDurationList[i].available && (this.availableDurationList[i].startTime.indexOf(date) != -1)){
						if(!flag){
							flag = true
							this.startIndex = i
						}
						this.startTimeList.push(this.availableDurationList[i].startTime.replace('T',' '))
					}
					
				}
			},
			getEndTimeList(startTime){
				this.endTimeList = []
				this.indexEnd = 0
				console.log("currentDate",this.currentDate)
				console.log("indexStart",this.indexStart)
				for (var i = this.indexStart; i < this.availableDurationList.length; i++) {
					if(this.availableDurationList[i].startTime.indexOf(startTime) != -1){
						this.startIndex = i
						break
					}
				}
				for (var i = this.startIndex; i < this.availableDurationList.length; i++) {
					if((!this.availableDurationList[i].available) || (this.availableDurationList[i].endTime.indexOf(this.currentDate) == -1)){
						break
					}else{
						this.endTimeList.push(this.availableDurationList[i].endTime.replace('T',' '))
					}
				}
			},
			getMeetingRoomAva(roomId,date){
				let _this = this
				uni.request({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/meeting-room/show-with-available-duration/` + roomId,
					header: {
						'Authorization': this.token,
					},
					method:'GET',
					success: function (res) {
						if(res.data.code == 200){
							_this.availableDurationList = res.data.data.availableDurationList
							_this.getStartTimeList(date)
							_this.getEndTimeList(_this.startTimeList[0].replace(' ','T'))
						}
					},
					fail: (res) => {
						console.log("查询会议室失败")
					}
				})
			},
			bindPickerChange: function(e) {
			            this.indexStart = e.target.value
						this.getEndTimeList(this.startTimeList[this.indexStart].replace(' ','T'))
			        },
			bindPickerChangeEnd: function(e) {
			            this.indexEnd = e.target.value
			        },
			timeChange: function(e) {
						this.currentDate = e
						this.getMeetingRoomAva(this.roomId,this.currentDate)
			        },
			getCurrentInfo() {
				if(this.currentDate == ''){
					return -1
				}
				let curDate = Date.parse(new Date())
				let timeSum = Date.parse(this.startTimeList[this.indexStart].replace(/-/g, '/')) - curDate
				if(timeSum < 300000){
					return -2
				}
				//获取预约总时间
				for (var i = this.startIndex; i < this.availableDurationList.length; i++) {
					if(this.availableDurationList[i].endTime.indexOf(this.endTimeList[this.indexEnd].replace(' ','T')) != -1){
						this.endIndex = i
						break
					}
				}
				let hours = Math.ceil((this.endIndex - this.startIndex + 1) * 0.5)
				let allPrice = hours * this.price
				let strTemp = this.startTimeList[this.indexStart].split(' ')[1] + "~" + this.endTimeList[this.indexEnd].split(' ')[1]
				// let allPrice = 0
				return {
					time: strTemp,
					currentDate: this.currentDate,
					allPrice: allPrice,
					unitPrice:this.price,
					hours:hours,
				}
			}
		}
	}
</script>

<style>
	.price-container {
		width: 100%;
		overflow: hidden;
		font-size: 0;
		position: relative;
	}

	.price-container:after {
		position: absolute;
		z-index: 10;
		right: 0;
		bottom: 0;
		left: 36rpx;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.3);
		transform: scaleY(.3);
		background-color: #666666;
	}

	.container-price {
		display: inline-block;
		float: left;
		height: 90rpx;
		width: 30%;
	}

	.price-left {
		margin-left: 36rpx;
		font-size: 0;
		height: 100%;
		line-height: 90rpx;
	}


	.nav-jq0 {
		transform: scale(0.8);
		display: inline-block;
		font-weight: 800;
		position: relative;
		top: 1rpx;
	}

	.container-price text {
		font-size: 28rpx;
		font-weight: 800;
		color: red;
	}

	.container-peopleNumber-data {
		height: 90rpx;
		display: inline-flex;
		flex-flow: column;
		width: 70%;
		overflow: hidden;
		font-size: 24rpx;
	}

	.price-peopleNmber {
		flex-grow: 1;
		text-align: right;
		margin-right: 36rpx;
	}

	.price-peopleNmber text {
		vertical-align: middle
	}

	.price-data {
		margin-right: 36rpx;
		flex-grow: 1;
		text-align: right;
	}

	.price-data text {
		vertical-align: middle
	}
</style>
